package tc.alert.tckejisoc.es;

import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;
import tc.alert.tckejisoc.domain.EventBase;


/**
 * 单条写入
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class EventSingleWriter {
    private final ElasticsearchRestTemplate restTemplate;
    private int retryCount;

    public void saveSingle(EventBase event, String indexName) {
        try {

            IndexCoordinates index = IndexCoordinates.of(indexName);

            // 保存到指定索引
            restTemplate.save(event, index);
        } catch (ElasticsearchException e) {
            log.error("单条写入失败 event_id={}", event.getEventId(), e);

        }
    }
}